package global.com.loansapplication.ui.actviity.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.Bind;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.bean.SettingInfo;
import global.com.loansapplication.comm.Constants;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.event.SelectCityEvent;
import global.com.loansapplication.ui.actviity.login.biz.SelectInfoBiz;
import global.com.loansapplication.ui.actviity.login.component.DaggerSetecltInfoComponent;
import global.com.loansapplication.ui.actviity.login.contract.SelectInfoContract;
import global.com.loansapplication.ui.actviity.login.module.SelectInfoModule;
import global.com.loansapplication.ui.actviity.login.presenter.SelectInfoPresenter;
import global.com.loansapplication.ui.widget.wheel.ChangeAddressDialog;
import global.com.loansapplication.util.AlertUtils;
import global.com.loansapplication.util.LogUtils;
import global.com.loansapplication.util.PreferencesUtils;
import global.com.loansapplication.util.StringUtils;
import rx.functions.Action1;

public class SelcetInfoAct extends BaseActivity implements SelectInfoContract.View {


    @Bind(R.id.select_info_city)
    LinearLayout city;
    @Bind(R.id.select_info_custom)
    LinearLayout custom;
    @Bind(R.id.select_info_daikun)
    LinearLayout daikun;
    @Bind(R.id.select_info_car)
    LinearLayout car;
    @Bind(R.id.select_info_room)
    LinearLayout room;
    @Bind(R.id.select_info_card)
    LinearLayout card;
    @Bind(R.id.select_info_social)
    LinearLayout social;
    @Bind(R.id.select_info_gongjijin)
    LinearLayout gongjijin;
    @Bind(R.id.select_info_baodan)
    LinearLayout baodan;
    @Bind(R.id.select_info_submit)
    TextView submit;

    @Inject
    SelectInfoPresenter presenter;

    @Inject
    SelectInfoBiz bia;
    @Bind(R.id.select_info_city_text)
    TextView cityText;
    @Bind(R.id.select_info_custom_text)
    TextView customText;
    @Bind(R.id.select_info_daikun_text)
    TextView daikunText;
    @Bind(R.id.select_info_car_text)
    TextView carText;
    @Bind(R.id.select_info_room_text)
    TextView roomText;
    @Bind(R.id.select_info_card_text)
    TextView cardText;
    @Bind(R.id.select_info_social_text)
    TextView socialText;
    @Bind(R.id.select_info_gongjijin_text)
    TextView gongjijinText;
    @Bind(R.id.select_info_baodan_text)
    TextView baodanText;

    private String[] Str = {"不限", "上班族", "企业主", "个体户"};
    private List<String> list = new ArrayList<String>();
    private String[] Str1 = {"不限", "3个月", "6个月", "12个月", "24个月", "36个月"};
    private List<String> list1 = new ArrayList<String>();
    private ChangeAddressDialog mChangemouthsDialog;
    private ChangeAddressDialog mChangecustomDialog;
    SettingInfo settingInfo = new SettingInfo();
    String type = "";

    private String[] StrInfo = {"不限", "有", "没有"};

    @Override
    protected void setRoot() {
        setTitleBar("#FFFE782F");
        setContentView(R.layout.activity_select_info);
    }

    @Override
    protected void setup() {
        setToolBar("自定义筛选", "");
        DaggerSetecltInfoComponent.builder().selectInfoModule(new SelectInfoModule(this)).build().inject(this);
        presenter.setBiz(bia);
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        intUI();
        RxView.clicks(city)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startAct(SelectCityAct.class, null);
                    }
                });

        RxView.clicks(car)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setCarInfo(carText);
                    }
                });
        RxView.clicks(room)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setRoomInfo(roomText);
                    }
                });

        RxView.clicks(card)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setCardInfo(cardText);
                    }
                });

        RxView.clicks(social)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setSocialInfo(socialText);
                    }
                });

        RxView.clicks(gongjijin)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setGongjijinInfo(gongjijinText);
                    }
                });

        RxView.clicks(baodan)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setBaodanInfo(baodanText);
                    }
                });


        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.toMian(settingInfo, type);
                    }
                });
        RxView.clicks(custom)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setCustomInfo();
                    }
                });
        RxView.clicks(daikun)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setMouthInfo();
                    }
                });


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityEvent(SelectCityEvent info) {
        LogUtils.d("---------------------------------------->");
        if (info.getCity().length() > 0) {
            settingInfo.setCityIds(info.getCity().substring(0, info.getCity().length() - 1));
            cityText.setText(info.getCity().substring(0, info.getCity().length() - 1));
        }else{
            cityText.setText("不限");
            settingInfo.setCityIds("");
        }


    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }


    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bia.onDestory();
    }

    private void setCustomInfo() {
        mChangecustomDialog = new ChangeAddressDialog(
                SelcetInfoAct.this, list, "请选择客户类型");
        mChangecustomDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        customText.setText(list.get(position));
                        settingInfo.setCustomerType(position);
                        mChangecustomDialog.dismiss();
                        mChangecustomDialog = null;
                    }

                    @Override
                    public void cancel() {
                        mChangecustomDialog.dismiss();
                        mChangecustomDialog = null;
                    }
                });
        mChangecustomDialog.show();
    }

    private void setMouthInfo() {
        mChangemouthsDialog = new ChangeAddressDialog(
                SelcetInfoAct.this, list1, "请选择贷款时间");
        mChangemouthsDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        daikunText.setText(list1.get(position));
                        settingInfo.setMonth(position);
                        mChangemouthsDialog.dismiss();
                        mChangemouthsDialog = null;
                    }

                    @Override
                    public void cancel() {
                        mChangemouthsDialog.dismiss();
                        mChangemouthsDialog = null;
                    }
                });
        mChangemouthsDialog.show();
    }


    /**
     * 汽车
     *
     * @param textView
     */
    private void setCarInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethavecar(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }

    /**
     * 房子
     *
     * @param textView
     */
    private void setRoomInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethavehouse(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }


    /**
     * 信用卡
     *
     * @param textView
     */
    private void setCardInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethaveCreditCard(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }

    /**
     * 是否有社保
     *
     * @param textView
     */
    private void setSocialInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethaveShebao(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }

    /**
     * 是否有公积金
     *
     * @param textView
     */
    private void setGongjijinInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethaveGongjijin(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }

    /**
     * 保单
     *
     * @param textView
     */
    private void setBaodanInfo(final TextView textView) {
        AlertView checkAlert = AlertUtils.checkDoubleInfo(this, new AlertUtils.AlertViewControl() {
            @Override
            public void onItemClickListener(int positions) {
                if (positions == 0) {
                    textView.setText("不限");
                } else if (positions == 1) {
                    textView.setText("有");
                } else if (positions == 2) {
                    textView.setText("没有");
                }
                settingInfo.sethaveBaodan(positions);
            }
        }, "不限", "有", "没有");
        checkAlert.show();
    }

    private void intUI() {
        Constants.registerActivity.add(this);
        type = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? "" : getIntent().getStringExtra(Constants.KEYSTRING);
        list = Arrays.asList(Str);
        list1 = Arrays.asList(Str1);
        String setInfosStr = PreferencesUtils.getStringPreferences("settingInfo",
                "settingInfo", null);
        if (!TextUtils.isEmpty(setInfosStr)) {
            SettingInfo settingInfo = new Gson().fromJson(setInfosStr,
                    new TypeToken<SettingInfo>() {
                    }.getType());
            MyApplication.settingInfo = settingInfo;
        }

        if (MyApplication.settingInfo == null) {
            settingInfo.setCityIds("");
            settingInfo.setCustomerType(0);
            settingInfo.setMonth(0);
            settingInfo.sethavecar(0);
            settingInfo.sethavehouse(0);
            settingInfo.sethaveCreditCard(0);
            settingInfo.sethaveShebao(0);
            settingInfo.sethaveGongjijin(0);
            settingInfo.sethaveBaodan(0);
            settingInfo.sethaveYibao(0);
        } else {
            settingInfo.setCityIds(StringUtils.isEmpty(MyApplication.settingInfo.getCityIds()) ? "" : MyApplication.settingInfo.getCityIds());
            settingInfo.setCustomerType((MyApplication.settingInfo.getCustomerType() != null && Pattern.matches("^[0-3]", MyApplication.settingInfo.getCustomerType().toString())) ? MyApplication.settingInfo.getCustomerType() : 0);
            settingInfo.setMonth((MyApplication.settingInfo.getMonth() != null && Pattern.matches("^[0-5]", MyApplication.settingInfo.getMonth().toString())) ? MyApplication.settingInfo.getMonth() : 0);
            settingInfo.sethavecar((MyApplication.settingInfo.gethavecar() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethavecar().toString())) ? MyApplication.settingInfo.gethavecar() : 0);
            settingInfo.sethavehouse((MyApplication.settingInfo.gethavehouse() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethavehouse().toString())) ? MyApplication.settingInfo.gethavehouse() : 0);
            settingInfo.sethaveCreditCard((MyApplication.settingInfo.gethaveCreditCard() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveCreditCard().toString())) ? MyApplication.settingInfo.gethaveCreditCard() : 0);
            settingInfo.sethaveShebao((MyApplication.settingInfo.gethaveShebao() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveShebao().toString())) ? MyApplication.settingInfo.gethaveShebao() : 0);
            settingInfo.sethaveGongjijin((MyApplication.settingInfo.gethaveGongjijin() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveGongjijin().toString())) ? MyApplication.settingInfo.gethaveGongjijin() : 0);
            settingInfo.sethaveBaodan((MyApplication.settingInfo.gethaveBaodan() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveBaodan().toString())) ? MyApplication.settingInfo.gethaveBaodan() : 0);
            settingInfo.sethaveYibao((MyApplication.settingInfo.gethaveYibao() != null && Pattern.matches("^[0-2]", MyApplication.settingInfo.gethaveYibao().toString())) ? MyApplication.settingInfo.gethaveYibao() : 0);
        }

        if (!StringUtils.isEmpty(settingInfo.getCityIds())) {
            cityText.setText(settingInfo.getCityIds());
        }


        customText.setText(Str[settingInfo.getCustomerType()]);
        daikunText.setText(Str1[settingInfo.getMonth()]);
        carText.setText(StrInfo[settingInfo.gethavecar()]);
        roomText.setText(StrInfo[settingInfo.gethavehouse()]);
        cardText.setText(StrInfo[settingInfo.gethaveCreditCard()]);
        socialText.setText(StrInfo[settingInfo.gethaveShebao()]);
        gongjijinText.setText(StrInfo[settingInfo.gethaveGongjijin()]);
        baodanText.setText(StrInfo[settingInfo.gethaveBaodan()]);
    }


}


